<?php

use Illuminate\Support\Carbon;

if (!function_exists('custom_path_file')) {

    function custom_path_file($path, $suffix = '.php'):array{
        $files = [];
        if (is_dir(base_path().'/'.$path)) {
            foreach (scandir(base_path().'/'.$path) as $dir) {
                if ('.' !== $dir[0]) {
                    if (is_dir(base_path().'/'.$path.'/'.$dir)) {
                        $files = array_merge($files, custom_path_file($path.'/'.$dir));
                    } else {
                        if (substr($dir, -4) == $suffix) {
                            $files[] = $path.'/'.$dir;
                        }
                    }
                }
            }
        }

        return $files;
    }
}

if(!function_exists('custom_account_type')){

    function custom_account_type($data):string{
        if(!empty($data) && (is_string($data) || is_integer($data))){
            if(preg_match("/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/",$data)){
                return 'email';
            }else if(preg_match("/^[1]([3-9])[0-9]{9}$/",$data)){
                return 'phone';
            }else{
                return 'username';
            }
        }

        return 'username';
    }
}

if(!function_exists('custom_second_trans')){

    function custom_second_trans(int $seconds) : string {
        $d = floor($seconds / (3600*24));
        $h = floor(($seconds % (3600*24)) / 3600);
        $m = floor((($seconds % (3600*24)) % 3600) / 60);
        if($d>'0'){
            $times  = $d.'天'.$h.'小时'.$m.'分钟';
        }else{
            if($h!='0'){
                $times = $h.'小时'.$m.'分钟';
            }else{
                if($m!=0){
                    $times = $m.'分钟';
                }else{
                    $times = $seconds.'秒';
                }
            }
        }

        return $times;
    }
}

if(!function_exists('custom_random_code')){

    function custom_random_code(int $length = 6):string {
        return substr(str_shuffle("012345678901234567890123456789"), 0, $length);
    }
}

if(!function_exists('custom_get_string_number')){

    function custom_get_string_number(string $string) : int {
        $string = trim($string);

        if (empty($string)) return 0;

        $result = '';

        for ($i=0;$i<strlen($string);$i++){
            if (is_numeric($string[$i])){
                $result .= $string[$i];
            }
        }

        return intval($result);
    }
}

if(!function_exists('custom_return_success')){

    function custom_return_success($data=null) : array {

        return [
            'code' => 200,
            'msg'  => 'success',
            'data' => $data
        ];
    }
}

if(!function_exists('custom_return_error')){

    function custom_return_error($data=null) : array {

        return [
            'code' => 500,
            'msg'  => 'error',
            'data' => $data
        ];
    }
}

if (!function_exists('custom_create_user_number')){

    function custom_create_user_number(int $number,$length = 8) : string {
        $number = (string) $number;

        $numberLength = length($number);

        if ($numberLength < $length){

            $supplementString = '';

            for ($i=0;$i< $length - $numberLength;$i++){
                $supplementString += '0';
            }

            return $supplementString.$number;
        }

        return $number;
    }
}

if (!function_exists('customer_get_date')){
    function customer_get_date(int $days): string {

        if ($days == 0) return Carbon::now()->format('Y-m-d');

        $count = 0;
        $i = 0;

        while ($count < $days){

            if (Carbon::now()->addDays($i)->isWeekday()){
                $count ++;
            }

            $i ++;
        }

        dd($i);

        return Carbon::now()->addDays($i)->format('Y-m-d');
    }
}

